// OSR b Framework

package org.jvcs.fmwk;

import org.jvcs.common.*;
import org.jvcs.utils.*;
import org.jvcs.fmwk.*;

import java.io.*;
import java.net.*;
import java.util.*;

// Begin Program
public class jvcsProtocol {
	
	// States of the protocol
	public static final int START = 4;
	public static final int WAITING = 10;
	public static final int IDENTIFY = 11;
	public static final int RECV = 12;
    public static final int SENT_MESSAGE = 20;
    public static final int SENT_FILE = 30;
    public static final int DISCUSSING = 40;
	
	public static int currState = START;
	
	public static final String Q_HELLO = "Hello";
	public static final String Q_HI = "Hi";
	public static final String Q_SEND_AUTH_DETAILS = "send_auth_details";
	public static final String Q_AUTH_DETAILS = "auth_details";
	public static final String Q_AUTH_SUCCESS = "auth_success";
	public static final String Q_AUTH_FAILURE = "auth_failure";
	
	
	public static final String Q_CREATE_HUB = "create_hub";
	public static final String Q_HUB_CREATED = "hub_created";
	public static final String Q_HUB_CREATION_FAILURE = "hub_creation_failure";
	public static final String Q_CREATE_PROJECT = "create_project";
	public static final String Q_FILE_TRANSFER_PORT = "file_transfer_port";
	public static final String Q_FILE_UPLOAD_PORT = "file_upload_port";
	public static final String Q_PROJECT_CREATED = "project_created";
	public static final String Q_PROJECT_CREATION_FAILURE = "project_creation_failure";
	public static final String Q_FILE_WRITTEN = "file_written";
	public static final String Q_TAKE_FILE = "take_file";
	public static final String Q_DOWNLOADING_FILE = "downloading_file";
	public static final String Q_DELIVERING_NOW = "deliver_now";
	public static final String Q_UPLOAD_FILE_NOW = "upload_file_now";
	public static final String Q_CREATE_USER = "create_user";
	public static final String Q_USER_CREATED = "user_created";
	public static final String Q_USER_CREATION_FAILURE = "user_creation_failure";
	public static final String Q_CREATE_GROUP = "create_group";
	public static final String Q_GROUP_CREATED = "group_created";
	public static final String Q_GROUP_CREATION_FAILURE = "group_creation_failure";
	public static final String Q_CREATE_BRANCH = "create_branch";
	public static final String Q_BRANCH_CREATED = "branch_created";
	public static final String Q_BRANCH_CREATION_FAILURE = "branch_creation_failure";
	public static final String Q_CREATE_TAG = "create_tag";
	public static final String Q_TAG_CREATED = "tag_created";
	public static final String Q_TAG_CREATION_FAILURE = "branch_creation_failure";
	
	public static final String Q_SEND_FILE = "send_file";
	public static final String Q_UPLOADING_FILE = "upload_file";
	public static final String Q_RECEIVING_FILE = "receiving_file";
	public static final String Q_CLIENT_SEND_FILE = "client_send_file";
	public static final String Q_SERVER_RECV_FILE = "server_recv_file";
	public static final String Q_START_FILE_RECV = "start_file_recv";
	public static final String Q_SEND_FILE_MD5SUM = "send_file_md5sum";
	public static final String Q_START_FILE_MD5SUM_RECV = "start_file_md5sum_recv";
	public static final String Q_QUIT = "quit";
	public static final String Q_BYE = "bye";
	
	public int fsPortNo = 0;
	
	public String respond(String request) {
		String response = null;
		if (request == null) {
			response = Q_HELLO;
		}
		if (currState == WAITING) {
			response = Q_HELLO;
		}
		if (request.equals(Q_HI)) {
			response = Q_SEND_AUTH_DETAILS;
		}
		if (request.startsWith(Q_AUTH_DETAILS)) {
			String [] tmp = request.split(G.T_fs);
			String result = Q_AUTH_FAILURE; // default send failure
			int tmpl = tmp.length;
			String auth_details = null;
			if (tmpl == 2) {
				auth_details = tmp[1];
				String [] stmp = auth_details.split(G.T_rfs);
				int stmpl = stmp.length;
				String userName = null;
				String passwd = null;
				if (stmpl == 2) {
					userName = stmp[0];
					passwd = stmp[1];
					if (userName.equals("osr") && passwd.equals("osr"))  {
						result = Q_AUTH_SUCCESS;
					}
				}
			}
			response = result;
		}
		if (request.startsWith(Q_CREATE_HUB)) {
			fsPortNo = GeneralUtils.getRandomInt(1050, 22000);	
			osr.pl("File Recv Port: "+fsPortNo);
			response = Q_FILE_UPLOAD_PORT+"|"+fsPortNo;;
		}
		if (request.startsWith(Q_CREATE_PROJECT)) {			
			fsPortNo = GeneralUtils.getRandomInt(1050, 22000);			
			osr.pl("File Transfer Port: "+fsPortNo);
			response = Q_FILE_TRANSFER_PORT+"|"+fsPortNo;
		}
		if (request.startsWith(Q_DOWNLOADING_FILE)) {
			osr.pl("CONNECTING ON PORT: "+fsPortNo);
			ServerTransfer sr = new ServerTransfer(fsPortNo);
			sr.start();
			response = Q_DELIVERING_NOW+G.T_fs+fsPortNo;
		}
		if (request.startsWith(Q_UPLOADING_FILE)) {
			osr.pl("RECEIVING ON PORT: "+fsPortNo);
			ServerRecv sr = new ServerRecv(fsPortNo);
			sr.start();
			response = Q_UPLOAD_FILE_NOW+G.T_fs+fsPortNo;
		}
		if (request.startsWith(Q_FILE_WRITTEN)) {
			response = Q_BYE;
		}	
		if (request.startsWith(Q_CREATE_USER)) {
			response = Q_USER_CREATED;
		}
		if (request.startsWith(Q_CLIENT_SEND_FILE)) {			
			response = Q_SERVER_RECV_FILE;
		}
		if (request.equals(Q_SEND_FILE_MD5SUM)) {
			response = Q_START_FILE_MD5SUM_RECV;
		}
		if (request.equals(Q_BYE)) {
			response = Q_BYE;
			currState = WAITING;
		}
		
		return response;
	}
	
	

}
// End Program